#include "chatwidget.h"
#include "ui_chatwidget.h"
#include <QScrollBar>

ChatWidget::ChatWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ChatWidget)
{
    ui->setupUi(this);



    m_chatBox = new ChatBox(this);
    m_chatBox->setObjectName("chatBox");
    int splitterOffsetY = 60;
    int chatBoxHeight = 300;
    m_splitter = new QSplitter(Qt::Vertical, this);
    m_splitter->setObjectName("verticalSplitter");
    m_splitter->addWidget(m_chatBox);
    m_splitter->addWidget(ui->inputChatMessage);
    m_splitter->setHandleWidth(1);

    m_chatBox->setMinimumHeight(200);
    ui->inputChatMessage->setMinimumHeight(100);
    QList<int> sizes;
    sizes << 600 << 400;
    m_splitter->setSizes(sizes);
    m_chatBox->setGeometry(QRect(0, 0, this->width(), m_chatBox->height()));
    ui->inputChatMessage->setGeometry(QRect(0, ui->inputChatMessage->y(), this->width(), m_splitter->height()-m_chatBox->height()));
    m_splitter->show();
    m_splitter->setGeometry(QRect(0, splitterOffsetY, this->width(), this->height()-splitterOffsetY+10));
    connect(m_splitter, &QSplitter::splitterMoved, [=](int pos, int index) {
        m_chatBox->setGeometry(QRect(0, 0, m_chatBox->width(), pos));
        ui->inputChatMessage->setGeometry(QRect(0, pos, ui->inputChatMessage->width(), this->height() - pos));
        // QScrollBar* scrollbar = m_chatBox->verticalScrollBar();
        // scrollbar->setValue(scrollbar->maximum()); // 滚动到最底部
        // QListWidgetItem* lastItem = m_chatBox->item(m_chatBox->count() - 1);
        // m_chatBox->scrollToItem(lastItem, QAbstractItemView::EnsureVisible);
    });

    ui->btnSendMsg->raise();  // 提升到最前
}

ChatWidget::~ChatWidget()
{
    delete ui;
}

QPushButton* ChatWidget::getSendMsgBtn() const
{
    return ui->btnSendMsg;
}

ChatBox* ChatWidget::getChatBox() const
{
    return m_chatBox;
}

QTextEdit* ChatWidget::getChatInputBox() const
{
    return ui->inputChatMessage;
}

QPushButton* ChatWidget::getVideoCallBtn() const
{
    return ui->BtnVideoCall;
}

QLabel* ChatWidget::getLabelFriendName() const
{
    return ui->LabelChatName;
}

// protected:

void ChatWidget::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);

    // int inputBoxHeight = ui->inputChatMessage->height();
    // ui->inputChatMessage->resize(this->width() - 20, inputBoxHeight);
    // ui->inputChatMessage->move(10, 10);

    int xOffset = 20;
    int yOffset = 7;
    ui->LabelChatName->move(xOffset, yOffset);
    xOffset = 200;
    yOffset = 10;
    ui->BtnVideoCall->move(xOffset, yOffset);


    int interval = 10;
    ui->btnSendMsg->move(this->width() - ui->btnSendMsg->width() - interval, this->height() - ui->btnSendMsg->height() - interval);

    if (m_splitter)
    {
        int splitterOffsetY = 60;

        m_splitter->setGeometry(QRect(0, m_splitter->y(), this->width(), this->height()-splitterOffsetY+10));
    }
    m_chatBox->setGeometry(QRect(0, m_chatBox->y(), this->width(), m_chatBox->height()));
    m_chatBox->refreshUI();
    ui->inputChatMessage->setGeometry(QRect(0, ui->inputChatMessage->y(), this->width(), ui->inputChatMessage->height()));
}


